package com.example.administrator.jingpingou.shop_activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.bean.Shop_User;
import com.example.administrator.jingpingou.config.NetConfig;
import com.example.administrator.jingpingou.netinteface.NetInterface;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import cn.sharesdk.framework.ShareSDK;
import java.io.IOException;
import java.util.List;


import cn.sharesdk.onekeyshare.OnekeyShare;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ShopContentActivity extends AppCompatActivity {
    private Retrofit mRetrofit;
    private NetInterface mInterface;
    private WebView mWebView;
    private String path;
    private String shoptitle;
    private DbManager mDb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_content);
        DbManager.DaoConfig config = new DbManager.DaoConfig();
        mDb = x.getDb(config);
        initView();
        initData();

        mWebView.loadUrl(NetConfig.SHOP_DATA_PATH_TEST+path);
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setWebChromeClient(new WebChromeClient());
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
    }

    private void initData() {
        Intent intent = getIntent();
        path = intent.getStringExtra("path");
        shoptitle = intent.getStringExtra("shoptitle");
    }

    private void initView() {
        mWebView = (WebView) findViewById(R.id.web_content_show);
    }

    public void onClick(View view) {
        if(view!=null) {
            switch (view.getId()) {
                case R.id.iv_shop_content_back :
                    Toast.makeText(ShopContentActivity.this, "不能再往后了", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.iv_shop_content_go :
                    Toast.makeText(ShopContentActivity.this, "不能再往前了", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.iv_shop_content_refresh :
                    mWebView.reload();
                    break;
                case R.id.iv_shop_content_share :
                    showShare();
                    break;
                case R.id.iv_shop_content_shoucang:
                    Shop_User shopcontent = new Shop_User();
                    shopcontent.setShopname(shoptitle);
                    try {
                        mDb.save(shopcontent);
                        List<Shop_User> shopname = mDb.selector(Shop_User.class).where("shopname", "=", shoptitle).findAll();
                        if(shopname.size()!=0) {
                            Toast.makeText(ShopContentActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        }
                    } catch (DbException e) {
                        e.printStackTrace();
                    }

                    break;
            }
        }
    }

    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("这是我分享的标题");
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(this);

    }
}
